//
//  ZZCBaseCDVWKNavigationDelegate.swift
//  ewgShop
//
//  Created by zzc on 2019/8/7.
//  Copyright © 2019 zzc. All rights reserved.
//

import UIKit
import WebKit

class ZZCBaseCDVWKNavigationDelegate: NSObject ,WKNavigationDelegate{
    
   weak var viewController : ZZCCordovaBaseWebViewVC?
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        print("can here")
        let navigationActionPolicy = self.viewController?.webView(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler);

        if navigationActionPolicy == WKNavigationActionPolicy.cancel.rawValue || navigationActionPolicy == WKNavigationActionPolicy.allow.rawValue {
            decisionHandler(WKNavigationActionPolicy.init(rawValue: navigationActionPolicy!)!);
            return
        }
        let selector =  NSSelectorFromString("webView:decidePolicyForNavigationAction:decisionHandler:");
        if (self.viewController != nil && (((self.viewController?.webViewEngine as? WKNavigationDelegate) != nil)) && self.viewController!.webViewEngine!.responds(to: selector)){
            let webEngine = self.viewController!.webViewEngine! as! WKNavigationDelegate;
            webEngine.webView?(webView, decidePolicyFor: navigationAction, decisionHandler: decisionHandler)
        }else{
            decisionHandler(WKNavigationActionPolicy.allow);
        }
        
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        
        let navigationResponsePolicy = self.viewController?.webView(webView, decideResponsePolicyFor: navigationResponse, decisionHandler: decisionHandler)
        
        if navigationResponsePolicy == WKNavigationResponsePolicy.cancel.rawValue || navigationResponsePolicy == WKNavigationResponsePolicy.allow.rawValue {
            decisionHandler(WKNavigationResponsePolicy.init(rawValue: navigationResponsePolicy!)!)
            return
        }
        
        let selector =  NSSelectorFromString("webView:decidePolicyForNavigationResponse:decisionHandler:");
        if (self.viewController != nil && (((self.viewController?.webViewEngine as? WKNavigationDelegate) != nil)) && self.viewController!.webViewEngine!.responds(to: selector)){
            let webEngine = self.viewController!.webViewEngine! as! WKNavigationDelegate;
            webEngine.webView?(webView, decidePolicyFor: navigationResponse, decisionHandler: decisionHandler);
        }else{
            decisionHandler(WKNavigationResponsePolicy.allow);
        }

    }
    
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        self.viewController?.webView(webView, didStartProvisionalNavigation: navigation)
        let selector =  NSSelectorFromString("webView:didStartProvisionalNavigation:");
        if (self.viewController != nil && (((self.viewController?.webViewEngine as? WKNavigationDelegate) != nil)) && self.viewController!.webViewEngine!.responds(to: selector)){
            let webEngine = self.viewController!.webViewEngine! as! WKNavigationDelegate;
            webEngine.webView?(webView, didStartProvisionalNavigation: navigation)
        }
    }
    
    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
        
        self.viewController?.webView(webView, didReceiveServerRedirectForProvisionalNavigation: navigation)
        let selector =  NSSelectorFromString("webView:didReceiveServerRedirectForProvisionalNavigation:");
        if (self.viewController != nil && (((self.viewController?.webViewEngine as? WKNavigationDelegate) != nil)) && self.viewController!.webViewEngine!.responds(to: selector)){
            let webEngine = self.viewController!.webViewEngine! as! WKNavigationDelegate;
            webEngine.webView?(webView, didReceiveServerRedirectForProvisionalNavigation: navigation)
        }
        
    }
    
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        
        self.viewController?.webView(webView, didFailProvisionalNavigation: navigation, withError: error)
        let selector =  NSSelectorFromString("webView:didFailProvisionalNavigation:withError:");
        if (self.viewController != nil && (((self.viewController?.webViewEngine as? WKNavigationDelegate) != nil)) && self.viewController!.webViewEngine!.responds(to: selector)){
            let webEngine = self.viewController!.webViewEngine! as! WKNavigationDelegate;
            webEngine.webView?(webView, didFailProvisionalNavigation: navigation, withError: error)
        }
        
    }
    
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
    
        self.viewController?.webView(webView, didCommit: navigation)
        
        let selector =  NSSelectorFromString("webView:didCommitNavigation:");
        if (self.viewController != nil && (((self.viewController?.webViewEngine as? WKNavigationDelegate) != nil)) && self.viewController!.webViewEngine!.responds(to: selector)){
            let webEngine = self.viewController!.webViewEngine! as! WKNavigationDelegate;
            webEngine.webView?(webView, didCommit: navigation)
        }
    }
    
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.viewController?.webView(webView, didFinish: navigation)
        let selector =  NSSelectorFromString("webView:didFinishNavigation:");
        if (self.viewController != nil && (((self.viewController?.webViewEngine as? WKNavigationDelegate) != nil)) && self.viewController!.webViewEngine!.responds(to: selector)){
            let webEngine = self.viewController!.webViewEngine! as! WKNavigationDelegate;
            webEngine.webView?(webView, didFinish: navigation)
        }
    }
    
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        self.viewController?.webView(webView, didFail: navigation, withError: error)
        let selector =  NSSelectorFromString("webView:didFailNavigation:withError:");
        if (self.viewController != nil && (((self.viewController?.webViewEngine as? WKNavigationDelegate) != nil)) && self.viewController!.webViewEngine!.responds(to: selector)){
            let webEngine = self.viewController!.webViewEngine! as! WKNavigationDelegate;
            webEngine.webView?(webView, didFail: navigation, withError: error)
        }
    }
    
    func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
        self.viewController?.webViewWebContentProcessDidTerminate(webView)
        let selector =  NSSelectorFromString("webViewWebContentProcessDidTerminate:");
        if (self.viewController != nil && (((self.viewController?.webViewEngine as? WKNavigationDelegate) != nil)) && self.viewController!.webViewEngine!.responds(to: selector)){
            let webEngine = self.viewController!.webViewEngine! as! WKNavigationDelegate;
            webEngine.webViewWebContentProcessDidTerminate?(webView)
        }
    }
#if DEBUG
    func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        print("webView:didReceiveAuthenticationChallenge:completionHandler:");
        
        let selector =  NSSelectorFromString("webView:didReceiveAuthenticationChallenge:completionHandler:");
        if (self.viewController != nil && (((self.viewController?.webViewEngine as? WKNavigationDelegate) != nil)) && self.viewController!.webViewEngine!.responds(to: selector)){
            let webEngine = self.viewController!.webViewEngine! as! WKNavigationDelegate;
            webEngine.webView?(webView, didReceive: challenge, completionHandler: completionHandler)
        }
        
        if (challenge.protectionSpace.authenticationMethod as NSString).isEqual(to: NSURLAuthenticationMethodServerTrust) {
            if challenge.previousFailureCount == 0 {
                let credential = URLCredential.init(trust: challenge.protectionSpace.serverTrust!);
                completionHandler(URLSession.AuthChallengeDisposition.useCredential,credential);
            }else{
                completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge,nil);
            }
        }
        
        
    }
#endif
    
}
